package com.imoresoft.magic.util;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.data.Parameter;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.agile.entity.Result;
import com.taobao.api.domain.UserSubscribe;

public class UserSubscUtil {

	protected static final Logger logger = LoggerFactory.getLogger(UserSubscUtil.class);

	public static void main(String[] args) {
	}

	public static String builderOrderStr(Result result) {
		int daysLeft = DateUtil.daysBetween(new Date(), (Date) result
				.getResult());
		String endDate = DateUtil.format2YMD((Date) result.getResult());
		String orderInfo = result.getMsg() + "授权到" + endDate + "，还有" + daysLeft
				+ "天过期。";
		return orderInfo;
	}

	public static Result getUserClass(SuperDao dao, String userId,
			UserSubscribe userSubs) {
		Result taobaoVersion = getTaobaoVersion(userSubs);
		Result presentVersion = getPresentVersion(dao, userId);
		if (presentVersion.getCode() == -1 && taobaoVersion.getCode() == -1) {
			return taobaoVersion;
		}
		if (presentVersion.getCode() >= taobaoVersion.getCode()) {
			return presentVersion;
		} else {
			return taobaoVersion;
		}
	}

	public static Result getTaobaoVersion(UserSubscribe userSubs) {
		Result result = new Result(-1, "");
		if (userSubs == null || !"subscribeUser".equals(userSubs.getStatus())
				|| userSubs.getEndDate() == null
				|| userSubs.getEndDate().before(new Date())) {
			return result;
		}

		Long versionNo = userSubs.getVersionNo();
		if (versionNo == null || versionNo.intValue() <= 0) {
			return result;
		}
		int version = userSubs.getVersionNo().intValue();
		result.setCode(version);
		String versionName = null;
		switch (version) {
		case 1:
			versionName = "体验版";
			break;
		case 2:
			versionName = "标准版";
			break;
		case 3:
			versionName = "高级版";
			break;
		default:
			versionName = "未知版";
		}
		result.setMsg(versionName);
		result.setResult(userSubs.getEndDate());
		return result;
	}

	public static Result getPresentVersion(SuperDao dao, String userId) {
		Result result = new Result(-1, null);
		int presentVersion = -1;
		String tableName = "TF_S_USER";
		try {
			String sql = "select present_version,present_deadline from "
					+ tableName
					+ " where present_deadline > now() and user_id = ? "
					+ " and present_version >0";
			Parameter p = new Parameter();
			p.add(userId);
			IDataset dataset = dao.queryListBySql(sql, p);
			if (dataset.size() > 0) {
				IData dataMap = dataset.getData(0);
				String version = dataMap.getString("PRESENT_VERSION");
				presentVersion = new Double(version).intValue();
				result.setCode(presentVersion);
				result.setMsg("[赠送VIP" + presentVersion + "版]");
				Date presentDeadline = (Date) dataMap.get("PRESENT_DEADLINE");
				result.setResult(presentDeadline);
			}
		} catch (Exception e) {
			logger.error("获取conn或查询数据库异常", e);
		}
		return result;
	}

}
